home *** CD-ROM | disk | FTP | other *** search
- ///--------------------------------------------------------------------------------------
- // Simple.c
- ///--------------------------------------------------------------------------------------
-
- #include <SWIncludes.h> // Automatically include all SpriteWorld headers
- #include <SWGameUtils.h>
-
- #include "SWApplication.h"
- #include "Simple.h"
-
- #define kMaxFPS 30 // Keep the animation from going too fast
-
- #define kNumSprites 10
- #define kMaxSpriteMoveDelta 8
-
-
- SpriteWorldPtr gSpriteWorldP;
- SpriteLayerPtr gSpriteLayerP;
- SpritePtr gMasterBallSpriteP;
- WindowPtr gWindowP;
-
-
- ///--------------------------------------------------------------------------------------
- // main
- ///--------------------------------------------------------------------------------------
-
- void main(void)
- {
- Initialize(kNumberOfMoreMastersCalls);
- Randomize();
-
- if (SWHasSystem7())
- {
- SetCursor(*GetCursor(watchCursor));
-
- CreateWindow();
- SetUpSpriteWorld();
- CreateBallSprite();
-
- HideCursor();
-
- AddSprites();
- RunAnimation();
- CleanUp();
- }
- else
- {
- CantRunOnThisMachine();
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateWindow
- ///--------------------------------------------------------------------------------------
-
- void CreateWindow(void)
- {
- Rect windRect;
- RgnHandle mBarUpdateRgn;
-
- gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
-
- if (gWindowP != NULL)
- {
- MoveWindow(gWindowP, 0, 0, false);
- windRect = gWindowP->portRect;
-
- // Center window in screen
- CenterRect(&windRect, &qd.screenBits.bounds);
- MoveWindow(gWindowP, windRect.left, windRect.top, false);
-
- ShowWindow(gWindowP);
- SetPort(gWindowP);
- mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
- EraseRgn(mBarUpdateRgn);
- }
- else
- CantFindResource();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetUpSpriteWorld
- ///--------------------------------------------------------------------------------------
-
- void SetUpSpriteWorld(void)
- {
- OSErr err;
- PixPatHandle pixPatH;
-
- // Initialize the SpriteWorld package
- err = SWEnterSpriteWorld();
- FatalError(err);
-
- // Create the SpriteWorld
- err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP, NULL, NULL, 0);
- FatalError(err);
-
- // Create the Sprite Layers
- err = SWCreateSpriteLayer(&gSpriteLayerP);
- FatalError(err);
-
- // Put the pieces together and lock the SpriteWorld.
- // Note: since we are locking the SpriteWorld before adding the sprites,
- // we must make sure to lock each sprite individually when creating them.
- SWAddSpriteLayer(gSpriteWorldP, gSpriteLayerP);
- SWLockSpriteWorld(gSpriteWorldP);
-
- // Load the background pattern
- pixPatH = GetPixPat(128);
-
- // Draw it in the background
- if (pixPatH != NULL)
- {
- SWSetPortToBackground(gSpriteWorldP);
- FillCRect(&gSpriteWorldP->backRect, pixPatH);
- DisposePixPat(pixPatH);
- }
-
- // Limit the frame rate to a specific speed
- SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateBallSprite - load the master ball sprite and prepare it for cloning later
- ///--------------------------------------------------------------------------------------
-
- void CreateBallSprite(void)
- {
- OSErr err;
-
- err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterBallSpriteP, NULL,
- 128, 1, kFatMask);
- FatalError(err);
-
- // Set up the sprite (remember that it must be locked!)
- SWLockSprite(gMasterBallSpriteP);
- SWSetSpriteMoveBounds(gMasterBallSpriteP, &gSpriteWorldP->backRect);
- SWSetSpriteMoveProc(gMasterBallSpriteP, BallSpriteMoveProc);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AddSprites - clone the master sprite, and add the clones to the SpriteWorld
- ///--------------------------------------------------------------------------------------
-
- void AddSprites(void)
- {
- SpritePtr newSpriteP;
- short spriteNum;
- OSErr err;
-
- for (spriteNum = 0; spriteNum < kNumSprites; spriteNum++)
- {
- // Clone the master sprite. The clone doesn't need to be locked,
- // since the master sprite was already locked.
- err = SWCloneSprite(gMasterBallSpriteP, &newSpriteP, NULL);
- FatalError(err);
-
- SWAddSprite(gSpriteLayerP, newSpriteP);
-
- SWSetSpriteLocation(newSpriteP,
- GetRandom(0, gSpriteWorldP->backRect.right-44),
- GetRandom(0, gSpriteWorldP->backRect.bottom-44) );
-
- do
- {
- SWSetSpriteMoveDelta(newSpriteP,
- GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta),
- GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta));
- } while (newSpriteP->horizMoveDelta == 0 || newSpriteP->vertMoveDelta == 0);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RunAnimation
- ///--------------------------------------------------------------------------------------
-
- void RunAnimation(void)
- {
- // Make sure CopyBits, if used, doesn't try to colorize things
- SWSetPortToWindow(gSpriteWorldP);
- ForeColor(blackColor);
- BackColor(whiteColor);
-
- SWUpdateSpriteWorld(gSpriteWorldP, true);
-
- while (!Button())
- {
- SWProcessSpriteWorld(gSpriteWorldP);
- SWAnimateSpriteWorld(gSpriteWorldP);
- }
-
- SWShowMenuBar((WindowPtr)gWindowP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CleanUp - This function is not really necessary, since the system will dispose
- // everything automatically when the program quits. However, if you sync the animation
- // to the VBL, you must either call SWDisposeSpriteWorld, or SWSyncSpriteWorldToVBL
- // with a value of false before your program quits, in order to remove the VBL task.
- ///--------------------------------------------------------------------------------------
-
- void CleanUp(void)
- {
- // Dispose the master sprite, since it isn't included in the SpriteWorld
- SWDisposeSprite(&gMasterBallSpriteP);
-
- // Dispose the SpriteWorld, including all sprites and layers currently in it
- SWDisposeSpriteWorld(&gSpriteWorldP);
- SWExitSpriteWorld();
-
- FlushEvents(everyEvent, 0);
- InitCursor();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // BallSpriteMoveProc
- ///--------------------------------------------------------------------------------------
-
- SW_FUNC void BallSpriteMoveProc(SpritePtr ballSpriteP)
- {
- SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
- (void)SWBounceSprite(ballSpriteP);
- }
-